<template>
  <ContentWrap title="数字资源版权信息">
    <el-form
      ref="queryFormRef"
      :inline="true"
      :model="queryParams"
      class="-mb-15px"
      label-width="72px"
    >
      <el-form-item label="版权编号:" prop="name">
        <el-input
          v-model="queryParams.name"
          class="!w-240px"
          clearable
          disabled
          placeholder=""
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="著作权人:" prop="copyrightNumber">
        <el-input
          v-model="queryParams.copyrightNumber"
          class="!w-240px"
          clearable
          placeholder="请输入著作权人"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <br />
      <el-form-item label="版权期限:" prop="validity">
        <el-radio-group v-model="validity" class="!w-240px">
          <el-radio label="always" size="large">永久版权</el-radio>
          <el-radio label="notAlways" size="large">非永久版权</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="选择时间:" prop="time">
        <el-date-picker
          v-model="value3"
          type="datetime"
          placeholder="选择时间"
          :default-time="defaultTime"
        />
      </el-form-item>
      <el-divider />
      <span><h4>资源信息(必填)</h4></span>
      <el-form-item>
        <el-button @click="openForm('create')">
          <Icon class="mr-5px" icon="ep:plus" />
          添加资源
        </el-button>
      </el-form-item>
      <br />
      <el-table v-loading="loading" :data="list">
        <el-table-column align="center" label="ID" prop="id" :width="50" />
        <el-table-column align="center" label="资源名称" prop="name" />
        <el-table-column align="center" label="DOI" prop="type" />
        <el-table-column align="center" label="资源类型" prop="copyrightNumber" />
        <el-table-column align="center" label="所属库" prop="copyrightValidity" />
        <el-table-column align="center" label="操作">
          <template #default="scope">
            <el-button
              v-hasPermi="['system:role:delete']"
              link
              type="danger"
              @click="handleDelete(scope.row.id)"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-form>
    <br />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <CopyrightForm ref="formRef" @success="getList" />
</template>
<script lang="ts" setup>
import * as microCopyrightApi from '@/api/copyright/microCopyright'
// import CopyrightForm from './CopyrightForm.vue'
import { ref } from 'vue'
const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const total = ref(0) // 列表的总页数
const list = ref([]) // 列表的数据
const validity = ref('always') // 版权期限
const value3 = ref('') // 时间选择器
const defaultTime = new Date(2000, 1, 1, 12, 0, 0) // 时间选择器
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  name: '21321XXXX',
  copyrightNumber: '',
  copyrightStatus: '',
  createTime: []
})
const queryFormRef = ref() // 搜索的表单

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await microCopyrightApi.getMicroCopyrightPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await microCopyrightApi.deleteCopyright(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 重置按钮操作 */
const handleActive = async () => {
  return message.success(t('功能未开发！'))
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>
